package ivan.cheng.upgrade;

import ivan.cheng.utils.LogUtil;
import ivan.cheng.utils.PreferencesUtils;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.telephony.TelephonyManager;
import android.text.format.Time;

public class Upgrade
{
	private Context mContext;
	private boolean mbRetry = false;// 有些手机需要二次联网激活
	private UpgradeCheckListener mUpgradeCheckListener = null;
	private void HandleError(String strErrno) 
	{
		//
	}
	
	public Upgrade(Context context)
	{
		mContext = context;
	}
	public Upgrade(Context context,UpgradeCheckListener upgradeCheckListener)
	{
		mContext = context;
		mUpgradeCheckListener = upgradeCheckListener;
	}
	
	private void SaveToDB(UpgradeInfo ui) {
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_RESULT, ui.result);
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_AVAILABLE_VERSION,
				ui.available_version);
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_CRC, ui.crc);
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_LAST_CHECK_TIME,
				ui.last_check_time);
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_LENGTH, ui.length);
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_TIP, ui.tip);
		PreferencesUtils.setValue(mContext, PreferencesUtils.KEY_URL, ui.url);
		// PreferencesUtils.setValue(this, PreferencesUtils.KEY_WAITING_INFOS,
		// ui.result);
	}

	private UpgradeInfo LoadUpgradeInfoFromDB() {
		UpgradeInfo ui = new UpgradeInfo();
		ui.result = PreferencesUtils.getIntValue(mContext,
				PreferencesUtils.KEY_RESULT);
		ui.length = PreferencesUtils.getIntValue(mContext,
				PreferencesUtils.KEY_LENGTH);
		ui.last_check_time = PreferencesUtils.getLongValue(mContext,
				PreferencesUtils.KEY_LAST_CHECK_TIME);
		ui.available_version = PreferencesUtils.getValue(mContext,
				PreferencesUtils.KEY_AVAILABLE_VERSION);
		ui.crc = PreferencesUtils.getValue(mContext, PreferencesUtils.KEY_CRC);
		ui.tip = PreferencesUtils.getValue(mContext, PreferencesUtils.KEY_TIP);
		ui.url = PreferencesUtils.getValue(mContext, PreferencesUtils.KEY_URL);
		ui.waiting_infos = PreferencesUtils.getValue(mContext,
				PreferencesUtils.KEY_WAITING_INFOS);
		return ui;
	}

	
	public boolean CheckIfWeNeedUpgrage(boolean forceCheck) {
		/**
		 * we get the last check time first to check if we need check this time
		 */
		UpgradeInfo ui = LoadUpgradeInfoFromDB();
		Time time = new Time();
		time.setToNow();
		long current_time = time.toMillis(false);
		LogUtil.i("current_time:" + current_time + "last_check_time:"
				+ ui.last_check_time);
		if (current_time - ui.last_check_time < 23 * 60 * 60 * 1000 && false == forceCheck) {// we had
			LogUtil.i("we had checked in last 23 hour");
			return false;
		}
		int iVersionCode = 0;
		try {
				iVersionCode = mContext.getPackageManager().getPackageInfo
			(mContext.getPackageName(), 0).versionCode;
		} 
		catch (NameNotFoundException e) 
		{
			LogUtil.w(e.toString());
			return false;
		}
		TelephonyManager mTelephonyMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		String sImsi = mTelephonyMgr.getSubscriberId();
//		new CheckIfWeNeedUpgrageTask().execute(this, String.valueOf(Setting.PROJECT_ID), String.valueOf(Setting.APP_ID), String
//				.valueOf(65535 + iVersionCode), sImsi, "2", "0");
		return true;
	}
}

